<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in cities" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="submit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
// 获取所有角色信息
import { getRoles } from '@/api/settings'
// 查询当前用户角色信息
import { getUserDetailById } from '@/api/user'
// 保存分配角色
import { assignRoles } from '@/api/employees'
export default {
  props: {
    curEmployeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 选中的id数据
      roleIds: [],
      // 角色数据列表
      cities: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    closeDialog() {
      this.$emit('close')
    },
    // 拉取所有角色数据
    async loadRoles() {
      const res = await getRoles({ page: 1, pagesize: 100 })
      console.log(res)
      // 绑定角色数据
      this.cities = res.data.rows
      // 获取当前角色信息
      const info = await getUserDetailById(this.curEmployeId)
      console.log(info)
      // 将选中的数据回填
      this.roleIds = info.data.roleIds
    },
    // 确定提交
    async submit() {
      const res = await assignRoles({ id: this.curEmployeId, roleIds: this.roleIds })
      console.log(res)
      // 分配角色成功
      this.$message.success(res.message)
      // 关闭对话框
      this.closeDialog()
    }
  }
}
</script>
